package com.linsir.lin.controller.rbac;

import com.linsir.lin.dto.RoleDTO;
import com.linsir.lin.pojo.Account;
import com.linsir.lin.pojo.BaseCode;
import com.linsir.lin.pojo.Role;
import com.linsir.lin.service.AccountService;
import com.linsir.lin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：linsir
 * @date ：Created in 2021/7/1 9:51
 * @description：账户权限相关页面跳转
 * @modified By：
 * @version: 1.0.0
 */

@Controller
@RequestMapping(value = "/admin")
public class RBACController {


    @Autowired
    private AccountService accountService;

    @Autowired
    private RoleService roleService;


    /**
     * 账号列表
     * @return
     */
    @RequestMapping(value = "/account",method= {RequestMethod.GET})
    public String account()
    {
        return "rbac/account";
    }

    /**
     * 账号添加
     * @return
     */
    @RequestMapping(value = "/account/add",method = {RequestMethod.GET})
    public String accountAdd()
    {
        return "rbac/account_add";
    }

    /**
     * 账号设置
     * @return
     */
    @RequestMapping(value = "/account/setting",method = {RequestMethod.GET})
    public String accountSetting()
    {
        return "rbac/account_setting";
    }

    /**
     * 账号角色添加
     * @param accountId
     * @return
     */
    @RequestMapping(value = "/account/addRoles/{accountId}",method = {RequestMethod.GET})
    public String accountAddRole(@PathVariable(value = "accountId") Integer accountId, ModelMap modelMap) throws InvocationTargetException, IllegalAccessException {
       Account account=accountService.getById(accountId);
       List<BaseCode> roleGroups = roleService.roleGroup();
       List<Role> roleList = roleService.allRole();
       List<RoleDTO> roleDTOList = roleService.getAccountAndCheck(accountId);
       modelMap.addAttribute("account",account);
       modelMap.addAttribute("roleGroups",roleGroups);
       modelMap.addAttribute("roleList",roleList);
       modelMap.addAttribute("roleDTOList",roleDTOList);
        return "rbac/account_addRoles";
    }



    /**
     * 角色
     * @return
     */

    @RequestMapping(value = "/role",method = {RequestMethod.GET})
    public String role()
    {
        return "rbac/role";
    }

    /**
     * 角色添加
     * @return
     */
    @RequestMapping(value = "/role/add",method = {RequestMethod.GET})
    public String roleAdd()
    {
        return "rbac/role_add";
    }

    /**
     * 权限
     * @return
     */
    @RequestMapping(value = "/permission",method = {RequestMethod.GET})
    public String permission()
    {
        return "rbac/permission";
    }

}
